 //
//  PicVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PicVC.h"
#import "PicModel.h"
#import "ListModel.h"
#import "PicClassVC.h"
#import "PicTVCell.h"
#import "LastVC.h"
#import "HUDView.h"

@interface PicVC ()<UITableViewDataSource,UITableViewDelegate>

{
    int _count;
}

/**
 *  所有的
 */

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HUDView *HUD;

@property (nonatomic, strong) NSMutableArray *tempArr;



@end

@implementation PicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [super creatTopView];
    self.tempArr = [NSMutableArray array];
    _count = 1;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"图片";
    
//    小菊花
    HUDView *hud = [[HUDView alloc]init];
    [self.view addSubview:hud];
    self.HUD = hud;
    
    
//  数据解析
    [self afn];
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(afn)];
    
  
  /*
    NSURLCache *cache = [NSURLCache sharedURLCache];
//    内存缓存大小
    cache.memoryCapacity = 2 * 1024 * 1024;
//    磁盘缓存大小
    cache.diskCapacity = 20 * 1024 * 1024;
    
    
//    内存、磁盘、服务器
//    获取数据的缓存
    NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
    if (response) {
        //        NSLog(@"data:%@", response.data);
        id cacheResult = [NSJSONSerialization JSONObjectWithData:response.data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"cacheResult: %@", cacheResult);
    }
   
   */
}



    
    
    
    
// 创建跳转到分类页按钮
-(void)buttonCreate
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(W * 20, H * 550, 30, 30);
    [button setImage:[UIImage imageNamed:@"iconfont-fenlei"] forState:UIControlStateNormal];
    button.alpha = 0.8;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = [[UIColor whiteColor] CGColor];
    [button addTarget:self action:@selector(pushClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

//数据解析
-(void)afn
{
    NSString *last = @"token=ARk6MCqumCVjvSM1yEuacwNu549OA7hujzU%3D";
    NSString *url = [NSString stringWithFormat:@"http://lab.daigj.com/api/ios/hm/index?version=1.0.3&pageNo=%d&pageSize=2&%@",_count,last];
    [AFN urlString:url getAFNdata:^(id responseObject) {
        
        NSMutableArray *listArray = responseObject [@"data"][@"data"][@"list"];

        for (NSDictionary *dic in listArray) {
            NSMutableArray *smallArr = [NSMutableArray array];
            NSArray *arr = dic[@"value"];
            for (NSDictionary *smallDic in arr) {
                PicModel *picModel = [[PicModel alloc] init];
                [picModel setValuesForKeysWithDictionary:smallDic];
                picModel.cid = [smallDic objectForKey:@"id"];
                [smallArr addObject:picModel];
            }
            NSDictionary *tempDic = @{@"key": dic[@"key"], @"value": smallArr};
            [self.tempArr addObject:tempDic];
        }
//        NSLog(@"tempArr:%@", _tempArr);

        
        [self creatTableView];
        [self.tableView reloadData];
        [self buttonCreate];
        [self.HUD removeFromSuperview];
        [self.tableView.header endRefreshing];
    }];
    _count ++;
}



// 跳转到分类页按钮方法
-(void)pushClick:(UIButton *)sender
{
    PicClassVC *sec = [[PicClassVC alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
}

// 创建tableView
-(void)creatTableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 108) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //    self.tableView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:self.tableView];
    }
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.tempArr[section][@"value"] count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.tempArr.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reused = @"cell";
    PicTVCell *cell = [tableView dequeueReusableCellWithIdentifier:reused];
    if (cell == nil) {
        cell = [[PicTVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reused];
    }
    
    [cell getVuale:self.tempArr[indexPath.section][@"value"][indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   NSString *timeString = self.tempArr[section][@"key"];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithRed:56./255. green:56./255. blue:56./255. alpha:1.];
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 44)];
    lbl.textAlignment = 1;
    lbl.textColor = [UIColor whiteColor];
    lbl.text = timeString;
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(W * 100, H * 8,W * 28,W * 28)];
    img.image = [UIImage imageNamed:@"iconfont-rili"];
    [view addSubview:img];
    
    [view addSubview:lbl];
    
    
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return HEIGHT / 4 * 2;;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicModel *mo = self.tempArr[indexPath.section][@"value"][indexPath.row];
    LastVC *last = [[LastVC alloc]init];
    [self.navigationController pushViewController:last animated:YES];
    last.getId = mo.cid;

}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
